<?php

namespace Admin\Controller;


use Admin\Common\Page;

class LogFreenumController extends BaseController{

    public function index(){

//触发器：
//DELIMITER $
//create trigger update_card_trigger
//after update
//ON card
//FOR EACH ROW
//begin
//declare card_id,delivery_old,delivery_new,delivery_change,activity_old,activity_new,activity_change int;
//set card_id = new.card_id;
//
//set delivery_old = old.num_delivery;	-- 原传送章
//set delivery_new = new.num_delivery;	-- 新传送章
//set delivery_change = delivery_new-delivery_old;
//
//set activity_old = old.num_activity;	-- 原星星章
//set activity_new = new.num_activity;	-- 新星星章
//set activity_change = activity_new-activity_old;
//
//if delivery_change<>0 or activity_change<>0 then
//	insert into free_change (`card_id`,`delivery_old`,`delivery_new`,`delivery_change`,`activity_old`,`activity_new`,`activity_change`) values (card_id,delivery_old,delivery_new,delivery_change,activity_old,activity_new,activity_change);
//end if;
//end$
//DELIMITER ;


        $keyword = I('get.keyword');
        if (!empty($keyword)) {
            $where['a.free_change_id'] = $keyword;
            $where['a.card_id'] = $keyword;
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }

        $interval_time = I('get.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';
            $map['a.create_time'] = [
                ['gt',$time[0]],
                ['lt',$time[1]]
            ];
        }

        $type = I("get.type");
        if(!empty($type)){
            switch ($type){
                case 1:
                    $map['delivery_change'] = array('NEQ',0);
                    break;
                case 2:
                    $map['activity_change'] = array('NEQ',0);
                    break;
            }
        }

        $count = M('free_change')->alias('a')->where($map)->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $data = M('free_change')->alias('a')
            ->where($map)
            ->order('a.create_time desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        if(I("get.execl")==1){
            $file = excel_export($data);
            ouput_Browser($file);
        }else{
            $this->data = $data;
            $this->display();
        }

    }
}